﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiTemplate : ApiBase
    {
        public ApiTemplate(Controller ctx) : base(ctx) { }

        //系统模板
        public void Do(string action)
        {
            switch (action)
            {
                case "create_template":
                    {
                        string type_1 = GetStringParam(new ReqStringParamOption { Name = "type_1", Level = REQPARAMLEVEL.FREE });
                        string type_2 = GetStringParam(new ReqStringParamOption { Name = "type_2", Level = REQPARAMLEVEL.FREE });
                        string content_type = GetStringParam(new ReqStringParamOption { Name = "content_type", Level = REQPARAMLEVEL.FREE });
                        string title = GetStringParam(new ReqStringParamOption { Name = "title", Level = REQPARAMLEVEL.FREE });
                        string introduction = GetStringParam(new ReqStringParamOption { Name = "introduction" });
                        string content = GetStringParam(new ReqStringParamOption { Name = "content" });
                        string create_time = DateTimeEx.GetNowTimeStamp();
                        Template tp = new Template { title = title, type_1 = type_1, type_2 = type_2, content_type = content_type, content = content, introduction = introduction, create_time = DateTimeEx.GetNowTimeStamp() };
                        G.DB.Insert<Template>(tp);
                        string aa = tp.content;
                        ResponseJson(new Result());
                        break;
                    }
                case "delate_template":
                    {
                        if (G.DB.Delete<Template>(req_id) == 0) ThrowStatus(STATUS.FAILED_DELETE, "*模板删除失败！");
                        else ResponseJson(new Result());
                        break;                  
                    }
                case "change_template":
                    {
                        string type_2 = GetStringParam(new ReqStringParamOption { Name = "type_2" });
                        string content_type = GetStringParam(new ReqStringParamOption { Name = "content_type" });
                        string title = GetStringParam(new ReqStringParamOption { Name = "title", Level = REQPARAMLEVEL.FREE });
                        string content = GetStringParam(new ReqStringParamOption { Name = "content" });
                        string introduction = GetStringParam(new ReqStringParamOption { Name = "introduction" });                                             
                        Cd cd = new Cd();
                        Ob ob = new Ob();
                        Template tp = G.DB.GetById<Template>(req_id);
                        tp.type_2 = type_2;
                        if (title != "") tp.title = title;
                        tp.content = content;
                        tp.introduction = introduction;
                        tp.renovate_time = DateTimeEx.GetNowTimeStamp();
                        G.DB.Update<Template>(tp);
                        ResponseJson(new ObjectResult { data = tp });
                        break;
                    }
                case "search_template":
                    {
                        string type_1 = GetStringParam(new ReqStringParamOption { Name = "type_1", Level = REQPARAMLEVEL.FREE });
                        string type_2 = GetStringParam(new ReqStringParamOption { Name = "type_2", Level = REQPARAMLEVEL.FREE });
                        string content_type = GetStringParam(new ReqStringParamOption { Name = "content_type", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        cd.And("type_1 = '{0}'", type_1);
                        if (!"0".Equals(type_2) && "*" != type_2) cd.And("type_2={0}", type_2);
                        if (!"0".Equals(content_type) && "*" != content_type) cd.And("content_type = '{0}' ", content_type);
                        ResponseJson(GetPageResult<Template>(cd.Render(), G.DB.GetPrimaryKey<Template>()));
                        break;
                        //List<Template> list = Template.GetFirstTableList();
                        //Template t = Template.GetFirstEntity();
                        //ResponseJson(new Result { value = t });                 
                    }
                case "select_template":
                    {
                        int req_id = GetIntParam(new ReqIntParamOption { Name = "id" });
                        string type_1 = GetStringParam(new ReqStringParamOption { Name = "type_1" });
                        string type_2 = GetStringParam(new ReqStringParamOption { Name = "type_2", Level = REQPARAMLEVEL.FREE });
                        Template temp = G.DB.Get<Template>(new Cd().And("type_1 = '{0}'", type_1).And("type_2 = '{0}'", type_2).And("selected = 1").Render()).GetFirstEntity();
                        if (temp != null)
                        {
                            temp.selected = 0;
                            G.DB.Update<Template>(temp);
                        }
                        Template template = G.DB.GetById<Template>(req_id);
                        template.selected = 1;
                        ResponseJson(new BooleanResult { value = G.DB.Update<Template>(template) > 0 });
                        break;
                    }
            }
        }
    }
}